package com.qijian.common.config.jackson.serialize;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

public class BigDecimalSerialize extends JsonSerializer<BigDecimal> {

    public static final BigDecimalSerialize INSTANCE = new BigDecimalSerialize();

    @Override
    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
        if (value != null) {
            gen.writeString(value.stripTrailingZeros().toPlainString());
        }
    }

    @Override
    public Class<BigDecimal> handledType() {
        return BigDecimal.class;
    }
}
